package com.qf.votedemo.contestant.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.votedemo.contestant.entity.Contestant;
import com.qf.votedemo.contestant.entity.Contestantphone;
import com.qf.votedemo.contestant.entity.Ticket;
import com.qf.votedemo.contestant.mapper.ContestantphoneMapper;
import com.qf.votedemo.contestant.service.IContestantphoneService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;


/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lx
 * @since 2023-02-24
 */
@Service
public class ContestantphoneServiceImpl extends ServiceImpl<ContestantphoneMapper, Contestantphone> implements IContestantphoneService {

    @Autowired
    private ContestantphoneMapper contestantphoneMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 为选手投票
     *
     * @param pid
     * @param contestantId
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void vote(Integer pid, Integer contestantId) {
        Contestantphone contestantphone = new Contestantphone();
        contestantphone.setPid(pid);
        contestantphone.setCount(1);
        contestantphone.setCid(contestantId);
        contestantphone.setBallotTime(new Date().getTime());
        save(contestantphone);
    }

    /**
     * 更新选手票数
     *
     * @param contestantphone
     * @param pid
     * @param contestantId
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateVote(Contestantphone contestantphone, Integer pid, Integer contestantId) {

        update(contestantphone, new LambdaQueryWrapper<Contestantphone>().eq(Contestantphone::getPid, pid).eq(Contestantphone::getCid, contestantId));
    }

    @Override
    public List<Ticket> getTicket() {
        List<Ticket> tickets = contestantphoneMapper.getTicket();
        return tickets;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public Integer getTotal() {
        return contestantphoneMapper.getTotal();
    }

    @Override
    public List<Ticket> getTicketRedis() {
        List<Ticket> tickets = new ArrayList<>();
        Set<ZSetOperations.TypedTuple> typedTuples = redisTemplate.opsForZSet().rangeByScoreWithScores("vote:zset", 0, Double.MAX_VALUE);
        //获取全部选手名字
        List<Contestant> contestants = redisTemplate.opsForList().range("vote:contestant", 0, -1);
        Map<Integer, String> contestanMap = contestants.stream().collect(Collectors.toMap(Contestant::getCid, Contestant::getName));
        for (ZSetOperations.TypedTuple typedTuple : typedTuples) {
            Ticket ticket = new Ticket();
            Object key = typedTuple.getValue();
            Double score = typedTuple.getScore();
            String name = contestanMap.get(key);
            ticket.setName(name);
            ticket.setCount(score.intValue());
            tickets.add(ticket);
        }
        return tickets;
    }
}
